/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.dao.MenuItemDao;
import org.crystalrange.model.MenuItem;
import org.crystalrange.model.User;
import org.crystalrange.service.MenuItemService;
import org.crystalrange.service.UserService;

public class MenuItemServiceImpl implements MenuItemService {

    private MenuItemDao menuItemDao;
    private UserService userService;

    public void setMenuItemDao(MenuItemDao menuItemDao) {
        this.menuItemDao = menuItemDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public MenuItem create(String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        MenuItem menuItem = new MenuItem();
        menuItem.setCreatedBy(currentUser);
        menuItem.setCreatedDate(new Date());
        
        return menuItem;
    }
    
    public boolean save(MenuItem menuItem, String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        menuItem.setUpdatedBy(currentUser);
        menuItem.setUpdatedDate(new Date());
        
        return this.menuItemDao.save(menuItem);
    }

    public boolean remove(MenuItem menuItem) {
        return this.menuItemDao.remove(menuItem);
    }

    public MenuItem find(Long id) {
        return this.menuItemDao.find(id);
    }

    public List<MenuItem> findAll() {
        return this.menuItemDao.findAll();
    }

    public SearchResult<MenuItem> searchAndCount(Search search) {
        return this.menuItemDao.searchAndCount(search);
    }

    public List<MenuItem> search(Search search) {
        return this.menuItemDao.search(search);
    }
    
    public MenuItem searchUnique(Search search) {
        return this.menuItemDao.searchUnique(search);
    }
}
